function oBlur_1() {
    let a = document.getElementById("hostName").value;
    if (a == "") {
        document.getElementById("remind_1").innerHTML = "主机名字不能为空！".fontcolor("red");
        document.getElementById("change_margin_1").style.marginBottom = 1 + "px";
    } else {
        document.getElementById("remind_1").innerHTML = "";
        document.getElementById("change_margin_1").style.marginBottom = 19 + "px";
    }
}

function oBlur_2() {
    let b = document.getElementById("hostIp").value;
    if (b == "") {
        document.getElementById("remind_2").innerHTML = "主机IP不能为空！".fontcolor("red");
        document.getElementById("change_margin_2").style.marginBottom = 1 + "px";
    } else {
        document.getElementById("remind_2").innerHTML = "";
        document.getElementById("change_margin_2").style.marginBottom = 19 + "px";
    }
}

function oBlur_3() {
    let c = document.getElementById("hostType").value;
    if (c == "") {
        document.getElementById("remind_3").innerHTML = "系统类型不能为空！".fontcolor("red");
        document.getElementById("change_margin_3").style.marginBottom = 1 + "px";
    } else {
        document.getElementById("remind_3").innerHTML = "";
        document.getElementById("change_margin_3").style.marginBottom = 19 + "px";
    }
}

function oBlur_4() {
    let c = document.getElementById("hostState").value;
    if (c == "") {
        document.getElementById("remind_4").innerHTML = "主机简介不能为空！".fontcolor("red");
        document.getElementById("change_margin_4").style.marginBottom = 1 + "px";
    } else {
        document.getElementById("remind_4").innerHTML = "";
        document.getElementById("change_margin_4").style.marginBottom = 19 + "px";
    }
}

function oFocus_1() {
    document.getElementById("remind_1").innerHTML = "";
    document.getElementById("change_margin_1").style.marginBottom = 19 + "px";
}

function oFocus_2() {
    document.getElementById("remind_2").innerHTML = "";
    document.getElementById("change_margin_2").style.marginBottom = 19 + "px";
}

function oFocus_3() {
    document.getElementById("remind_3").innerHTML = "";
    document.getElementById("change_margin_3").style.marginBottom = 19 + "px";
}

function oFocus_4() {
    document.getElementById("remind_4").innerHTML = "";
    document.getElementById("change_margin_4").style.marginBottom = 19 + "px";
}

window.onload = function(){

    var index = parent.layer.getFrameIndex(window.name);

    $('#saveHost').click(function () {
    
        /*if(!submitTest())
            parent.layer.confirm('信息未全，不能添加！',{btn:['确定']});
        else {*/
        var hostName = $("#hostName").val();
        var hostIp = $("#hostIp").val();
        var hostType = $("#hostType").val();
        hostType = "default";
        var hostState = $("#hostState").val();
        var hostGroupId = $("#hostGroup").val().join(',');
        var re=/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/;//正则表达式
        var ip_true = true;
        if(re.test(hostIp)) {
            if( RegExp.$1<256 && RegExp.$2<256 && RegExp.$3<256 && RegExp.$4<256){
                ip_true = false;
            }
        }

      //  if(hostName==""||hostIp==""||hostType==""||hostState==""||hostGroupId==""){
        if(hostName==""||hostIp==""||hostType==""||hostState==""){
            parent.layer.confirm('关键信息未全，不能添加！',{btn:['确定']});
        }else if(ip_true){
            parent.layer.confirm('IP格式错误，不能添加！',{btn:['确定']});
        }else {
            $.ajax({
                url: constUrl + "MonitorHostServlet",
                type: 'post',
                data: {
                    operateType: "add",
                    hostName: hostName,
                    hostIp: hostIp,
                    // hostType: hostType,
                    introduction: hostState,
                    hostGroup: hostGroupId
                },
                dataType: 'json',
                success: function (dataText) {
                    console.log(dataText.result);
                    if (dataText.result) {
                        parent.layer.confirm('添加成功！', {
                            btn: ['确定']
                        }, function () {
                            parent.location.reload(index);
                            parent.layer.close(index);
                        })
                    }else {
                        parent.layer.confirm('创建失败！', {
                            btn: ['确定']
                        }, function () {
                            parent.layer.close(index);
                        })
                    }
                },

                complete: function () {
                },
                error: function () {

                }
            });
        }
    });

    $('#closeIframe').click(function(){
        parent.layer.close(index);
    });

    $.ajax({
        url:constUrl+"MonitorHostGroupServlet",
        type:'post',
        data:{
            operateType:"showAll"
        },
        dataType:'json',
        success: function(responseText) {
            var hostGroupHtml ='';
            for(var i=0;i<responseText.datas.length;i++){
                hostGroupHtml+='<option value="'+(responseText.datas[i].hostGroupId)+'" >'+responseText.datas[i].hostGroupName+'</option>'
            }
            $("#hostGroup").append(hostGroupHtml);
        },
        complete: function() {
        },
        error: function() {
        }
    });
}


function checkIP(value){
    var exp="/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/";
    var reg = value.match(exp);
    
    if(reg==null)
    {
    //alert("开始的IP地址不合法！");
    return false;
    }
}

jQuery.validator.addMethod("ip", function(value, element) {
	return this.optional(element) || (/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/.test(value) && (RegExp.$1 <256 && RegExp.$2<256 && RegExp.$3<256 && RegExp.$4<256));
	}, "请输入合法的IP信息"); 
